//
//  AdCellModel.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/29/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit


final class AdCellModel {
    
    var post: Post
    
    private let statusColors: [ADStatus: UIColor] = [
        .active: .green,
        .archived: .gray,
        .created: .blueColor
    ]
    
    private let statusTexts: [ADStatus: String] = [
        .active: "Активный",
        .archived: "Архив",
        .created: "В ожидании"
    ]
    
    @Price var priceText: String = ""
    
    var name: NSAttributedString {
        let marka = post.car.marka?.name_ru ?? ""
        let model = post.car.model?.name_ru ?? ""
        let generation = post.car.generation?.name ?? ""
        let serie = post.car.serie?.name ?? ""
        let modification = post.car.modification?.name ?? ""
        
        let textColor = self.postMarketing.colored ? UIColor.white : UIColor.black
        
        let attrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: textColor]
        let boldString = NSMutableAttributedString(string: marka, attributes: attrs)

        let text = " " + model + generation + " " + serie + " " + modification
        let attributedString = NSMutableAttributedString(string: text, attributes: [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15, weight: .regular),
            NSAttributedString.Key.foregroundColor: textColor
        ])
        
        boldString.append(attributedString)
        return boldString
    }
    
    var leftDaysText: String {
        let formatterData = FormatterDate()
        if let timestamp = post.timestamp {
            if let date = formatterData.getDate(inputDate: timestamp) {
                return formatterData.getLeftDays(timestamp: Int(date.timeIntervalSince1970), maxDays: 60)
            } else {
                return "Неизвестно"
            }
        } else {
            return "Неизвестно"
        }
    }
    
    var visText: String {
        return "\(post.visCount)"
    }
    
    var statusText: String {
        return statusTexts[post.status]!
    }
    
    var statusColor: UIColor {
        return statusColors[post.status]!
    }
    
    var isCellDarken: Bool {
        return post.status == ADStatus.archived
    }
        
    var postMarketing: PostReklama {
        return PostReklama(isTop: post.isTop, searchUp: post.isSearchUp, special: post.isSpecial, colored: post.isColored)
    }
    
    var wallpaper: String? {
        var image: String!
        if post.images != nil && post.images!.count > 0 {
            image = post.images![0].image
        }
        return image
    }
    
    init (post: Post) {
        self.post = post
        priceText = String(post.price)
    }
}

